package com.jing.controller;

import com.jing.pojo.PageBean;
import com.jing.pojo.ReadingRooms;
import com.jing.pojo.Result;
import com.jing.pojo.UpdateStateRequest;
import com.jing.service.ReadingRoomsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈书憬
 * @version 1.0
 * 2024/9/17
 * 自习室管理  增删改查
 */
@RestController
@RequestMapping("/readingRooms")
public class ReadingRoomsController {

    @Autowired
    private ReadingRoomsService readingRoomsService;


    /**
     * 分页管理->自习室列表
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param type
     * @return
     */
    @GetMapping
    public Result<PageBean<ReadingRooms>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String type
    ) {
        PageBean<ReadingRooms> pb = readingRoomsService.list(pageNum, pageSize, name, type);
        return Result.success(pb);
    }

    @GetMapping("/get")
    public Result<List<ReadingRooms>> listNoPage() {
        List<ReadingRooms> pb = readingRoomsService.listNoPage();
        return Result.success(pb);
    }


    /**
     * 分类类型管理，查找当前下拉列表的数据
     * @return
     */
    @GetMapping("/category")
    public Result<List<ReadingRooms>> categoryList() {
        List<ReadingRooms> cs = readingRoomsService.categoryList();
        return Result.success(cs);
    }


    /**
     * 添加自习室
     * @param readingRooms
     * @return
     */
    @PostMapping("/addReadingRooms")
    public Result add(@RequestBody @Validated ReadingRooms readingRooms) {
        readingRoomsService.add(readingRooms);
        return Result.success();
    }


    /**
     * 更新自习室
     * @param readingRooms
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody @Validated(ReadingRooms.Update.class) ReadingRooms readingRooms) {
        readingRoomsService.update(readingRooms);
        return Result.success();
    }


    /**
     * 根据指定ID删除自习室
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(Integer id) {
        readingRoomsService.deleteById(id);
        return Result.success();
    }


    /**
     * 跟新自习室状态
     * @param request
     * @return
     */
    @PutMapping("/updateState")
    public Result updateState(@RequestBody @Validated UpdateStateRequest request) {
        readingRoomsService.updateState(request.getId(), request.getState());
        return Result.success();
    }


}
